<?php

class Devices
{
 #constructor
    public function __construct($dbConnection)
    {
        $this->DBConnection=$dbConnection;
        $this->getAllDeviceTypes();
    }
    #end constructor
    private $DBConnection;
    public $DeviceTypes;
    public $DeviceTypesID;
    #properties
    
    #end properties

    #methods
    public function getAllDevices()
    {
        $devices = array();
        foreach ($this->DeviceTypesID as $id)
        {
            $devices = array_merge ($devices, $this->getAllofDeviceType($id));
   
        }
        return $devices;
    }
    
    
    public function getAllofDeviceType($deviceTypeID)
    {
        $devices = array();
        $queries = new mysqlqueries();
            $resultDevices = $this->DBConnection->query($queries->GetIndex($queries->Device, $deviceTypeID, "deviceType_ID", "name"));
            while ($rowDevices = mysqli_fetch_array($resultDevices)) 
            {      
                array_push($devices, new Device($rowDevices['ID'], $this->DBConnection));
            }
        
        return $devices;
    }
    
    public function getAllofRoom($roomID)
    {
        $deviceIDs = array();
        $devices = array();
        $queries = new mysqlqueries();
        
        $resultRelocationIS = $this->DBConnection->query($queries->GetIndex($queries->Relocation, $roomID, "roomIs_ID", "date DESC"));
        $resultRelocationShould = $this->DBConnection->query($queries->GetIndex($queries->Relocation, $roomID, "roomShould_ID", "date DESC"));
        $resultRelocationSame= $this->DBConnection->query($queries->GetIndex($queries->Relocation, array($roomID, $roomID), array("roomIs_ID", "roomShould_ID"), "date DESC"));    
        
        while ($rowRelocation = mysqli_fetch_array($resultRelocationSame)) 
        {      
            $add = true; 
            if(!in_array($rowRelocation['device_ID'], $deviceIDs))
            {
               array_push($deviceIDs, $rowRelocation['device_ID']);
               array_push($devices, new Device($rowRelocation['device_ID'], $this->DBConnection)); 
            }
        }
        while ($rowRelocation = mysqli_fetch_array($resultRelocationIS)) 
        {      
            $add = true; 
            if(!in_array($rowRelocation['device_ID'], $deviceIDs))
            {
               array_push($deviceIDs, $rowRelocation['device_ID']);
               array_push($devices, new Device($rowRelocation['device_ID'], $this->DBConnection)); 
            }
        }
        while ($rowRelocation = mysqli_fetch_array($resultRelocationShould)) 
        {      
            $add = true; 
            if(!in_array($rowRelocation['device_ID'], $deviceIDs))
            {
               array_push($deviceIDs, $rowRelocation['device_ID']);
               array_push($devices, new Device($rowRelocation['device_ID'], $this->DBConnection)); 
            }
        }

        return $devices;
    }
    
    public function getAllofLevel($levelID, roomAndLevel $roomAndLevelClass)
    {
        $devices = array();
        $roomPerLevel = $roomAndLevelClass->getAllRoomsPerLevel($levelID);
        foreach($roomPerLevel as $roomElement) 
        {
            $devices = array_merge( $devices, $this->getAllofRoom($roomElement['ID']));
        }
        return $devices; 
    }
    
    private function getAllDeviceTypes()
    {
        $deviceTypes = array();
        $deviceTypesID =array();
        $queries = new mysqlqueries();
        $deviceType = $this->DBConnection->query($queries->GetTable($queries->Devicetype,  null));
            while ($rowDeviceTyp = mysqli_fetch_array($deviceType)) 
            {      
                array_push($deviceTypes, $rowDeviceTyp['description']);
                array_push($deviceTypesID, $rowDeviceTyp['ID']);
            }   
            
    $this->DeviceTypes = $deviceTypes;
    $this->DeviceTypesID = $deviceTypesID; 
    }



    #end methods 
    //put your code here
}